package com.obtk.controllerSW;

import cn.hutool.core.lang.UUID;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.obtk.model.CstOrder;
import com.obtk.model.OrderLine;
import com.obtk.modelSGW.CstServer;
import com.obtk.modelSGW.ProStorage;
import com.obtk.service.CstOrderService;
import com.obtk.service.CustomerService;
import com.obtk.service.OrderLineService;
import com.obtk.util.CoreConst;
import com.obtk.util.PageUtil;
import com.obtk.util.ResultUtil;
import com.obtk.vo.base.PageResultVo;
import com.obtk.vo.base.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

/**
 * 功能说明
 *
 * @author Mr.Sun
 * @date 2023-03-31 08:49:48
 */
@Controller
@RequestMapping("/cstOrder")
public class CstOrderController {
    @Autowired
    private CstOrderService cstOrderService;
    @Autowired
    private OrderLineService orderLineService;
    @Autowired
    private CustomerService customerService;

    @PostMapping("/list")
    @ResponseBody
    public PageResultVo loadUsers(CstOrder cstOrder, Integer limit, Integer offset, Model model) {
        PageHelper.startPage(PageUtil.getPageNo(limit, offset), limit);
        List<CstOrder> cstOrdersList = cstOrderService.selectCstOrder(cstOrder);
        PageInfo<CstOrder> pages = new PageInfo<>(cstOrdersList);
        System.out.println(cstOrdersList);
        return ResultUtil.table(cstOrdersList, pages.getTotal());
    }
//    查询订单 传入编号和名字 以及 别的页面可以根据名字查询别的字段
    @PostMapping("/lists")
    @ResponseBody
    public ResponseVo loadUser(CstOrder cstOrder) {
        List<CstOrder> cstOrdersList = cstOrderService.selectCstOrder1(cstOrder);
        System.out.println(cstOrdersList);
        return ResultUtil.success("查询成功",cstOrdersList);
    }
    /**
     * 新增订单
     */
    @PostMapping("/insert")
    @ResponseBody
    public ResponseVo insert(CstOrder cstOrder, OrderLine orderLine, ProStorage proStorage) {
        System.out.println("孙伟"+orderLine);
        System.out.println("孙伟"+cstOrder);
        return  cstOrderService.insert(cstOrder,orderLine,proStorage);
    }

    /**
     * 编辑用户详情
     */
    @GetMapping("/edit")
    public String userDetail(Model model, String odrId) {
        CstOrder cstOrder = cstOrderService.selectByCstOrderId(odrId);
        System.out.println("controller" + odrId);
        model.addAttribute("cstOrder", cstOrder);
        return "cstOrder/update";
    }

    /**
     * 编辑用户
     */
    @PostMapping("/edit")
    @ResponseBody
    public ResponseVo editUser(CstOrder cstOrderForm) {
        int a = cstOrderService.updateByCstOrderId(cstOrderForm);
        if (a > 0) {
            return ResultUtil.success("编辑订单成功！");
        } else {
            return ResultUtil.error("编辑订单失败");
        }
    }

    /**
     * 删除商品
     */
    @GetMapping("/delete")
    @ResponseBody
    public ResponseVo deleteUser(String odrId) {
        List<String> cstOrderIdsList = Arrays.asList(odrId);
        int a = cstOrderService.updateStatusBatch(cstOrderIdsList, CoreConst.STATUS_INVALID);
        if (a > 0) {
            return ResultUtil.success("删除订单成功");
        } else {
            return ResultUtil.error("删除订单失败");
        }
    }

    @GetMapping("/batch/delete")
    @ResponseBody
    public ResponseVo batchDeleteUser(String odrIdStr) {
        String[] odrIds = odrIdStr.split(",");
        List<String> cstOrderIdsList = Arrays.asList(odrIds);
        int a = cstOrderService.updateStatusBatch(cstOrderIdsList, CoreConst.STATUS_INVALID);
        if (a > 0) {
            return ResultUtil.success("删除订单成功");
        } else {
            return ResultUtil.error("删除订单失败");
        }
    }
    @GetMapping("/pay")
    @ResponseBody
    public ResponseVo pay(CstServer cstServer,CstOrder cstOrder){
        System.out.println(cstServer);
        System.out.println(cstOrder);
        return cstOrderService.updateStatusByOdrCode(cstOrder,cstServer);
    }

}
